<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Address;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;

/**
 * 收获地址模块
 * Class AddressController
 * @package App\Http\Controllers\Api
 */
class AddressController extends Controller
{
    //添加地址  todo第一个地址时设置为默认。
    public function add_address(Request $request) {
        $validated_data = $request->validate([
            'api_token'         =>  'required|string',
            'title'             =>  'required|string',
            'district'          =>  'required|string',
            'detail'            =>  'required|string',
            'consignee'         =>  'required|string',
            'consignee_mobile'  =>  'required|string|size:11',
            'is_default'        =>  ['nullable', Rule::in(0,1)]
        ]);
        Log::info("address:".json_encode($request->all()));
        Log::info("validated_data:".json_encode($validated_data));
        Log::info("validated_data:".$validated_data['district']);
        $addressArr = explode('-',$validated_data['district']);
        if(!in_array($addressArr[0],['四川省','湖南省'])){//
            return $this->response(2,[],'非四川，长沙市域暂不开放，敬请期待！');
        }
        if($addressArr[0] == "湖南省" && $addressArr[1] != "长沙市"){
            return $this->response(2,[],'非四川，长沙市暂不开放，敬请期待！');
        }
        $user_model = new User();
        $user = $user_model->get_login_user($request->post('api_token'));
        unset($validated_data['api_token']);
        $validated_data['user_id'] = $user->id;

        Address::create($validated_data);

        return $this->response(0, [], "新增成功");
    }

    //地址列表
    public function address_list(Request $request) {
        $request->validate([
            'api_token' =>  'required|string'
        ]);

        $user_model = new User();
        $user = $user_model->get_login_user($request->get('api_token'));

        $list = Address::where('user_id', $user->id)->orderBy('id', 'desc')->get()->toArray();

        return $this->response(0, $list);
    }

    //查询单个地址
    public function address_info(Request $request) {
        $validated_data = $request->validate([
            'api_token'     =>  'required|string',
            'id'            =>  'nullable|int|gt:0',
            'is_default'    =>  'nullable|in:0,1'
        ]);

        $user_model = new User();
        $user = $user_model->get_login_user($request->get('api_token'));
        unset($validated_data['api_token']);
        $validated_data['user_id'] = $user->id;
        $address = Address::where($validated_data)->orderBy('id', 'desc')->first();

        return $this->response(0, $address ? $address->toArray() : []);
    }

    //更新单个地址
    public function update_address(Request $request) {
        $validated_data = $request->validate([
            'api_token'         =>  'required|string',
            'id'                =>  'required|int|gt:0',
            'title'             =>  'nullable|string',
            'district'          =>  'nullable|string',
            'detail'            =>  'nullable|string',
            'consignee'         =>  'nullable|string',
            'consignee_mobile'  =>  'nullable|string|size:11',
            'is_default'        =>  ['nullable', 'string', Rule::in('0', '1')]
        ]);

        $user_model = new User();
        $user = $user_model->get_login_user($request->get('api_token'));

        unset($validated_data['api_token'], $validated_data['id']);
        if (count($validated_data) == 0) {//无数据修改
            return $this->response(0);
        }
        $where = [
            'id'    =>  $request->get('id'),
            'user_id'   =>  $user->id
        ];
        if ($request->post('is_default')) {
            Address::where('user_id', $user->id)->update(['is_default' => '0']);
        }
        Address::where($where)->update($validated_data);
        return $this->response(0);
    }

    //删除单个地址
    public function delete_address(Request $request) {
        $validated_data = $request->validate([
            'api_token'         =>  'required|string',
            'id'                =>  'required|int|gt:0'
        ]);

        $user_model = new User();
        $user = $user_model->get_login_user($request->get('api_token'));

        $where = [
            'id'    =>  $request->get('id'),
            'user_id'   =>  $user->id
        ];

        Address::where($where)->delete();
        return $this->response(0);
    }
}
